package com.lq.cart.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lq.cart.entity.Cart;
import com.lq.cart.enums.CartCheckStatus;
import com.lq.common.api.client.ProductFeignClient;
import com.lq.cart.mapper.CartMapper;
import com.lq.cart.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lq.cart.vo.CartDetailVO;
import com.lq.common.api.dto.ProductDTO;
import com.lq.common.result.Result;
import com.lq.common.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 根据用户ID获取购物车详情列表
     * 
     * @param userId 用户ID
     * @return 购物车详情列表
     */
    @Override
    public List<CartDetailVO> getByUserId(Long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Cart> cartList = list(queryWrapper);
        List<CartDetailVO> cartDetailVOList = new ArrayList<>();
        // 遍历购物车列表，获取商品信息并组装成购物车详情VO
        for (Cart cart : cartList){
            Result<ProductDTO> result = productFeignClient.getProductById(cart.getProductId());
            if (result.getCode() == 200){
                ProductDTO productDTO = result.getData();
                CartDetailVO cartDetailVO = BeanUtil.copyProperties(productDTO, CartDetailVO.class);
                cartDetailVO.setProductId(productDTO.getId());
                cartDetailVO.setCartId(cart.getId());
                cartDetailVO.setUserId(cart.getUserId());
                cartDetailVOList.add(cartDetailVO);
            }
        }
        return cartDetailVOList;
    }

    @Override
    public boolean addProductToCart(Long productId) {
        // 从商品服务获取商品信息
        Cart cart = new Cart();
        cart.setUserId(Long.valueOf(UserContext.getUserId()));
        cart.setProductId(productId);
        cart.setQuantity(1);
        cart.setChecked(CartCheckStatus.CHECKED.getCode());
        cart.setCreateTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        return save(cart);
    }

    @Override
    public boolean updateCartQuantity(Long cartId, Integer quantity) {
        Cart cart = getById(cartId);
        if (cart != null) {
            cart.setQuantity(quantity);
            cart.setUpdateTime(LocalDateTime.now());
            return updateById(cart);
        }
        return false;
    }

    @Override
    public boolean updateCartChecked(Long cartId, Integer checked) {
        Cart cart = getById(cartId);
        if (cart != null) {
            cart.setChecked(checked);
            cart.setUpdateTime(LocalDateTime.now());
            return updateById(cart);
        }
        return false;
    }

    @Override
    public boolean removeCart(Long cartId) {
        return removeById(cartId);
    }

    @Override
    public List<Cart> getCheckedItemsByUserId(Long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("checked", 1);
        return list(queryWrapper);
    }

    @Override
    public CartDetailVO getCartDetail(Long cartId) {
        Cart cart = getById(cartId);
        CartDetailVO cartDetailVO = new CartDetailVO();
        Result<ProductDTO> productResult = productFeignClient.getProductById(cart.getProductId());
        if (productResult != null && productResult.getSuccess()) {
            // 解析商品信息并设置到购物车对象中
            ProductDTO productDTO = productResult.getData();
            cartDetailVO = BeanUtil.copyProperties(productDTO, CartDetailVO.class);
            cartDetailVO.setProductId(cart.getProductId());
            cartDetailVO.setCartId(cart.getId());
            cartDetailVO.setUserId(cart.getUserId());
        }

        return cartDetailVO;
    }
}